﻿using System.Web.Mvc;
using Domain.Enums;
using Domain.UnitOfWork;
using Mestedur.Models;
using Mestedur.ViewModels;
using Services;

namespace Mestedur.Controllers
{
    public class ResultController : Controller
    {
        readonly IResultService resultService;
        readonly IUserService userService;
        readonly IDatabaseService databaseService;
        readonly IUnitOfWorkFactory unitOfWorkFactory;

        public ResultController(IUnitOfWorkFactory unitOfWorkFactory, IResultService resultService, IUserService userService, IDatabaseService databaseService)
        {
            this.resultService = resultService;
            this.databaseService = databaseService;
            this.userService = userService;
            this.unitOfWorkFactory = unitOfWorkFactory;

        }
        //
        // GET: /Result/
        [Authorize]
        public ActionResult GetChapterResults(int id)
        {
            using (unitOfWorkFactory.Create())
            {
                ChapterResultsViewModel viewModel = new ChapterResultsViewModel();
                var user = userService.GetUserByName(User.Identity.Name);
                var chapter = databaseService.Chapters.Get(id);
                ViewData["ChapterId"] = chapter.Id;
                ViewData["Chapter"] = chapter.ChapterName;
                viewModel = ViewModelConverter.ConvertQuizToChapterResults(resultService.GetChapterResult(chapter, user));
                if (viewModel.RightAnswers > viewModel.WrongAnswers && viewModel.NotAnsweredQuestions == 0)
                {
                    viewModel.TestStatus = TestStatus.Passed;
                }
                else if (viewModel.RightAnswers <= viewModel.WrongAnswers && viewModel.NotAnsweredQuestions == 0)
                {
                    viewModel.TestStatus = TestStatus.Failed;
                }
                else
                {
                    viewModel.TestStatus = TestStatus.NotCompleted;
                }

                return View(viewModel);
            }
        }

        [Authorize]
        public ActionResult GetCourseResults(int id)
        {
            using (unitOfWorkFactory.Create())
            {
                var user = userService.GetUserByName(User.Identity.Name);
                var course = databaseService.Courses.Get(id);
                var viewModel = ViewModelConverter.ConvertQuizToCourseResults(resultService.GetCourseResult(course, user));
                return View(viewModel);
            }
        }

        [Authorize]
        public ActionResult GetTopicResults(int id)
        {
            using (unitOfWorkFactory.Create())
            {
                var user = userService.GetUserByName(User.Identity.Name);
                var topic = databaseService.Topics.Get(id);
                var viewModel = ViewModelConverter.ConvertQuizToTopicResults(resultService.GetTopicResult(topic, user));
                return View(viewModel);
            }
        }

    }
}
